package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.FinalStatement;
import com.zygh.policeasset.service.FinalStatementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/final-statement"})
@Api(tags = {"审计决算模块"})
@RestController
public class FinalStatementController {
    @Autowired
    FinalStatementService finalStatementService;

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("获取审计决算分页列表")
    public Object findAll(@RequestParam(required = false) String name,
                          @RequestParam(value = "status", required = false) Integer status,
                          @RequestParam(value = "companyId", required = false) Integer companyId,
                          @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                          @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize,
                          @RequestParam(required = false) String time1,
                          @RequestParam(required = false) String time2,
                          @RequestParam(required = false) Integer category,
                          @RequestHeader("findAll") boolean findAll,
                          @RequestHeader("userId") Integer userId) {
        if (findAll) {
            return this.finalStatementService.findAll(name, status, companyId, pageNum, pageSize, time1, time2, category, null);
        }
        return this.finalStatementService.findAll(name, status, companyId, pageNum, pageSize, time1, time2, category, userId);
    }

    @RequestMapping(value = {"/status"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "0 未提交 1 已提交 2通过 3未通过", required = true, paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query")})
    @ApiOperation("根据状态获取分页列表")
    public Object getByStatus(@RequestParam int status, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        if (StrUtil.isEmpty(String.valueOf(status))) {
            return new CommonResult().validateFailed("状态不能为空");
        }
        return this.finalStatementService.findByStatus(status, pageNum, pageSize);
    }

    @RequestMapping(value = {"/getproject"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加项目")
    public Object getProject(@RequestHeader("findAll") boolean findAll, @RequestHeader("userId") Integer userId) {
        if (findAll) {
            return this.finalStatementService.getProject(null);
        }
        return this.finalStatementService.getProject(userId);
    }

    @RequestMapping(value = {"/getadd"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加审计结算")
    public Object getAdd(@RequestParam String projectNo, @RequestHeader("findAll") boolean findAll, @RequestHeader("userId") Integer userId) {
        if (StrUtil.isEmpty(projectNo)) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (findAll) {
            return this.finalStatementService.getAdd(projectNo, null);
        }
        return this.finalStatementService.getAdd(projectNo, userId);
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加")
    public Object add(@RequestBody FinalStatement finalStatement) {
        if (StrUtil.isEmpty(finalStatement.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (StrUtil.isEmpty(finalStatement.getProjectName())) {
            return new CommonResult().validateFailed("项目名称不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(finalStatement.getSectionId()))) {
            return new CommonResult().validateFailed("标段id不能为空");
        }
        if (this.finalStatementService.add(finalStatement).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改")
    public Object update(@RequestBody FinalStatement params) {
        if (StrUtil.isEmpty(String.valueOf(params.getId()))) {
            return new CommonResult().validateFailed("ID不能为空");
        }
        if (this.finalStatementService.update(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.finalStatementService.remove(id).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }

    @RequestMapping(value = {"/findById"}, method = {RequestMethod.GET})
    @ApiOperation("根据id查询")
    public Object findById(@RequestParam int id) {
        return new CommonResult().success(this.finalStatementService.findById(Integer.valueOf(id)));
    }
}